<template>
  <div>
    <el-dialog :title="obj.isadd?'添加':'修改'" :visible.sync="obj.isshow" @closed="closed">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            :data="menudata"
            node-key="id"
            ref="tree"
            :props="{children: 'children',label: 'title'}"
            
            show-checkbox
          ></el-tree>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">{{obj.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { reqRoleadd, reqRoleinfo, reqRoledit } from "../../../request/api.js";
export default {
  props: ["obj", "menudata", "listdata"],
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: ""
      }
    };
  },
  methods: {
    // 取消form显示
    cancel() {
      this.obj.isshow = false;
      if (!this.obj.isadd) {
        this.form = {
          rolename: "",
          menus: "",
          status: ""
        };
        this.$refs.tree.setCheckedKeys([]);
      }
    },
    add() {
      if (this.obj.isadd) {
        this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
        reqRoleadd(this.form).then(res => {});
        this.$refs.tree.setCheckedKeys([]);
        this.form = {
          rolename: "",
          menus: "",
          status: ""
        };
      } else {
        this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
        reqRoledit(this.form).then(res => {});
      }
      this.$emit("refresh");
      this.obj.isshow = false;
    },
    change(id) {
      reqRoleinfo({ id: id }).then(res => {
        this.form = res.data.list;
        this.$refs.tree.setCheckedKeys(JSON.parse(res.data.list.menus));
        this.form.id = id;
      });
      this.$emit("refresh");
    },
    closed() {
      this.cancel();
    }
  }
};
</script>

<style>
</style>